<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <input type="text" v-model="num1" />
      <input type="text" v-model="num2" />
      <hr />
      <input type="button" value="加" @click="f(1)" />
      <input type="button" value="减" @click="f(2)" />
      <input type="button" value="乘" @click="f(3)" />
      <input type="button" value="除" @click="f(4)" />
      <h1>结果：<span v-model="result">{{ result }}</span></h1>
    </div>
    <!-- 本地引入vue.js框架文件 -->
    <script src="js/vue.js"></script>
    <script>
      let app = new Vue({
        el: "body>div",
        data: {
          result: "",
          num1: "",
          num2: "",
        },
        methods: {
          f(num) {
            if (isNaN(app.num1) || isNaN(app.num2)) {
              app.result = "输入错误!";
              return;
            }
            switch (num) {
              case 1:
                app.result = parseInt(app.num1) + parseInt(app.num2);
                break;
              case 2:
                app.result = app.num1 - app.num2;
                break;
              case 3:
                app.result = app.num1 * app.num2;
                break;
              case 4:
                app.result = app.num1 / app.num2;
            }
          },
        },
      });
    </script>
  </body>
</html>
